package com.technology.education.ui.common.withdraw

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.model.DurationWithdrawModel
import com.technology.education.data.model.ExchangeRuleModel
import com.technology.education.ext.getAppViewModel
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

class WithdrawViewModel : BaseViewModel() {
    /**
     * 0 退回保证金
     * 1 教师积分提现
     * 2 学生积分提现
     * 3 推广人员余额提现
     * 4 教师时长提现
     * 5.学生积分兑换时长
     */
    var type = 0
    val money = StringObservableField("0")
    val withdrawAccount = StringObservableField()
    val buttonText = StringObservableField("申请退回")
    val titleText = StringObservableField("退回保障金")
    val notifyText = StringObservableField("申请后七个工作日内退回,退回后将无法接单。")

    /**
     * 申请退回保障金
     */
    val applyResult = MutableLiveData<ResultState<DurationWithdrawModel>>()

    /**
     * 最大可提现金额
     */
    var maxMoney = 0

    override fun getData() {
        super.getData()
        request({
            when (type) {
                2 -> {
                    apiService.exchangeRule()
                }
                1, 4 -> {
                    apiService.exchangeRuleTeacher()
                }
                else -> {
                    apiService.exchangeRule()
                }
            }
        }, success = {
            when (type) {
                0 -> {
                    maxMoney = it.pointToMoneyValue
                    notifyText.set("申请后七个工作日内退回,退回后将无法接单。")
                }
                1, 2 -> {
                    maxMoney = it.pointToMoneyValue
                    notifyText.set("当前积分最多可兑换${it.pointToMoneyValue}元")
                }
                3 -> {
                    maxMoney= it.pointToDurationValue
                    notifyText.set("当前余额最多可提现${it.pointToDurationValue}元")
                }
                4 -> {
                    maxMoney = it.durationToMoneyValue.toInt()
                    notifyText.set("当前时长最多可兑换${it.durationToMoneyValue}元")
                }
                5 -> {
                    maxMoney = it.pointToDurationValue
                    notifyText.set("当前积分最多可兑换${it.pointToDurationValue} min")
                }

            }

        }, error = {
            toast(it.errorMsg)
        })
    }

    fun allMoney() {
        money.set(maxMoney.toString())
    }

    fun commit() {
        if (money.get().toInt() > maxMoney) {
            toast("当前余额不足")
            return
        }
        when (type) {
            0 -> applyDeposit()
            1 -> teacherWithdraw()
            3 -> spreaderWithdraw()
            4 -> teacherDurationWithdraw()
            5 -> studentPoint2Duration()
        }
    }

    /**
     * 推广员余额提现
     */
    private fun spreaderWithdraw() {
        request({
            apiService.spreaderWithdraw(
                hashMapOf(
                    "alipayName" to getAppViewModel().userInfo.value?.alipayName,
                    "alipayNo" to withdrawAccount.get(),
                    "value" to money.get()
                )
            )
        }, applyResult, isShowDialog = true)
    }

    /**
     * 学生积分兑换时长
     */
    private fun studentPoint2Duration() {
        request(
            { apiService.exchangeDuration(hashMapOf("value" to money.get())) },
            applyResult,
            isShowDialog = true
        )
    }


    /**
     * 教师时长提现
     */
    private fun teacherDurationWithdraw() {
        request({
            apiService.durationWithdraw(
                hashMapOf(
                    "alipayName" to getAppViewModel().userInfo.value?.alipayName,
                    "alipayNo" to withdrawAccount.get(),
                    "value" to money.get()
                )
            )
        }, applyResult, isShowDialog = true)
    }

    /**
     * 教师积分提现
     */
    private fun teacherWithdraw() {
        if (money.get().isEmpty()) {
            toast("请输入提现金额")
            return
        }
        if (money.get().toInt() > maxMoney) {
            toast("当前最大可提现金额为$maxMoney")
            return
        }
        request(
            {
                apiService.pointWithdrawTeacher(
                    hashMapOf(
                        "alipayName" to getAppViewModel().userInfo.value?.alipayName,
                        "alipayNo" to withdrawAccount.get(),
                        "value" to money.get()
                    )
                )
            },
            applyResult,
            isShowDialog = true
        )
    }


    private fun applyDeposit() {
        request({
            apiService.withdrawDeposit(
                hashMapOf(
                    "alipayName" to getAppViewModel().userInfo.value?.alipayName,
                    "alipayNo" to withdrawAccount.get(),
                    "value" to money.get().toDouble().toInt()
                )
            )
        }, applyResult, isShowDialog = true)
    }
}